/**
 * 
 */
package org.log4jeditor.xml.editor.validation;

import static org.junit.Assert.assertEquals;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.eclipse.core.resources.IResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.log4jconfig.xml.editor.validation.ContentValidator;
import org.log4jeditor.xml.editor.AbstractUITestCase;
import org.log4jeditor.xml.editor.DirtiesProjectFiles;

/**
 * @author Xi
 * 
 */
public class LayoutContentValidatorTestCase extends AbstractUITestCase {

	private ContentValidator validator;

	private static final Logger LOGGER = LogManager
	.getLogger(LayoutContentValidatorTestCase.class);

	@Before
	public void setup() throws Exception {
		LOGGER.debug("Starting setup");
		setupEditorConfiguration("log4j-validation-layout.xml");
		// remove all markers on document
		LOGGER.debug("Starting ...");
		file.deleteMarkers(ContentValidator.DEFAULT_MARKER_ID, true, IResource.DEPTH_INFINITE);
		file.deleteMarkers(ContentValidator.DEPRECATED_ELEMENT_MARKER_ID, true,IResource.DEPTH_INFINITE) ;
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		validator = new ContentValidator();
		validator.connect(document);
		LOGGER.debug("Setup done.");
	}

	@After
	public void tearDown() throws Exception {
		LOGGER.debug("Starting teardown");
		super.tearDownProject();
		validator.disconnect(document);
		LOGGER.debug("Teardown done");
	}

	@Test
	public void testScanAndValidateValidLayoutClass() throws Exception {
		LOGGER.debug("Starting test");
		int offset = getDocumentOffset(6, 24, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		LOGGER.debug("Test done");
	}

	@Test
	public void testScanAndValidateInvalidLayoutClass() throws Exception {
		int offset = getDocumentOffset(18, 26, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateEmptyLayoutClass() throws Exception {
		int offset = getDocumentOffset(12, 24, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateMissingLayoutClass() throws Exception {
		int offset = getDocumentOffset(25, 14, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateNewInvalidLayoutClass() throws Exception {
		int offset = getDocumentOffset(6, 24, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "org.apache.log4j.PatternLayout".length(), "unknown");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateNewEmptyLayoutClass() throws Exception {
		int offset = getDocumentOffset(6, 24, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "org.apache.log4j.PatternLayout".length(), "");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateFixInvalidLayoutClass() throws Exception {
		int offset = getDocumentOffset(18, 24, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "org.apache.log4j.UnknownPatternLayout".length(), "org.apache.log4j.PatternLayout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepInvalidLayoutClass() throws Exception {
		int offset = getDocumentOffset(18, 24, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "org.apache.log4j.UnknownPatternLayout".length(), "PatternLayout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepInvalidLayoutClass2() throws Exception {
		int offset = getDocumentOffset(18, 24, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "org.apache.log4j.UnknownPatternLayout".length(), "");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepInvalidLayoutClass3() throws Exception {
		int offset = getDocumentOffset(18, 24, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "org.apache.log4j.UnknownPatternLayout".length(), "test.TestAppender");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepValidLayoutClass() throws Exception {
		int offset = getDocumentOffset(6, 24, "layout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "org.apache.log4j.PatternLayout".length(), "test.TestLayout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

}
